package com.cpi.cornpp.plantversion.converter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.plantversion.dto.PlantVersionDTO;
import com.cpi.cornpp.plantversion.exception.PlantVersionException;
import com.cpi.cornpp.plantversion.view.PlantVersionGroupView;
import com.cpi.cornpp.plantversion.view.PlantVersionView;
import com.cs.common.utils.NumberUtil;

/**
 * Plant Version Converter
 * @author Edgar Joao
 *
 */
public class PlantVersionConverter {
	/**
	 * Plant Version Converter DTO to View
	 * @param dto
	 * @return
	 * @throws PlantVersionException
	 */
	public PlantVersionView converterPlantVersionDtoToView(PlantVersionDTO dto)throws PlantVersionException{
		PlantVersionView plantVersionView = null;
		try{
			if(dto != null){
				plantVersionView = new PlantVersionView();
				plantVersionView.setMonthPeriodId(dto.getMonthPeriodId());
				plantVersionView.setPlantVersionId(dto.getPlantVersionId());
				plantVersionView.setPlantCode(dto.getPlantShortName());
				plantVersionView.setStorageAmount(NumberUtil.convertTon(dto.getStorageAmount()));
				plantVersionView.setUnloadAmount(NumberUtil.convertTon(dto.getUnloadAmount()));
				plantVersionView.setGrind(NumberUtil.convertTon(dto.getGrind()));
				plantVersionView.setGrindProtectionDays(dto.getGrindProtectionDays());
			}
		}catch( Exception exception ){
			PlantVersionException plantVersionException=null;
			exception.printStackTrace(System.out);
			plantVersionException= new PlantVersionException(exception , CornPPException.LAYER_CONVERTER,CornPPException.ACTION_LISTS);
			throw plantVersionException;
		}	
		return plantVersionView;
	}
	/**
	 * 
	 * @param plantsVersionsDTOs
	 * @return
	 * @throws PlantVersionException
	 */
	public List convertDTOsToViews(List plantsVersionsDTOs) throws PlantVersionException{
		List plantsVersionGroupView = null;
		List monthPeriodsVersion = null;
		Iterator iterator = null;
		PlantVersionDTO plantVersionDTO = null;
		String plantCodeAnt = null;
		PlantVersionGroupView plantVersionGroupView = null;
		PlantVersionView plantVersionView = null;
		HashMap monthsPeriodsSumGrind = null;
		String grindKey = null;
		Double grindValue = null;
		try{
			monthsPeriodsSumGrind = new HashMap();
			plantsVersionGroupView = new ArrayList(0);
			iterator = plantsVersionsDTOs.iterator();
			while(iterator.hasNext()){
				plantVersionDTO =  (PlantVersionDTO) iterator.next();
				grindKey = Integer.toString(plantVersionDTO.getMonthPeriodId());
				grindValue = (Double)monthsPeriodsSumGrind.get(grindKey);
				if( grindValue != null ){
					grindValue = new Double(grindValue.doubleValue() + plantVersionDTO.getGrind());
				}else{
					grindValue = new Double(plantVersionDTO.getGrind());
				}
				monthsPeriodsSumGrind.put(grindKey, grindValue);
				if( plantCodeAnt == null || !plantVersionDTO.getPlantShortName().equals(plantCodeAnt) ){
					if( plantCodeAnt != null ){
						plantVersionGroupView = new PlantVersionGroupView();
						plantVersionGroupView.setPlantCode(plantCodeAnt);
						plantVersionGroupView.setMonthPeriodsVersion(monthPeriodsVersion);
						plantsVersionGroupView.add(plantVersionGroupView);
					}
					monthPeriodsVersion = new ArrayList(0);
				}
				plantVersionView = converterPlantVersionDtoToView(plantVersionDTO);
				monthPeriodsVersion.add(plantVersionView);
				plantCodeAnt = plantVersionDTO.getPlantShortName();
			}
			if( plantsVersionsDTOs.size() > 0 ){
				plantVersionGroupView = new PlantVersionGroupView();
				plantVersionGroupView.setPlantCode(plantCodeAnt);
				plantVersionGroupView.setMonthPeriodsVersion(monthPeriodsVersion);
				plantsVersionGroupView.add(plantVersionGroupView);
			}
			plantsVersionGroupView.add(monthsPeriodsSumGrind);
		}catch(Exception exception){
			PlantVersionException plantVersionException=null;
			exception.printStackTrace(System.out);
			plantVersionException= new PlantVersionException(exception , CornPPException.LAYER_CONVERTER,CornPPException.ACTION_LISTS);
			throw plantVersionException;
		}
		
		return plantsVersionGroupView;
	}
	/**
	 * Plant Version Converter Views To DTOs
	 * @author Edgar Joao
	 * @param view
	 * @return
	 * @throws PlantVersionException
	 */
	public PlantVersionDTO converterPlantVersionViewToDto(PlantVersionView view) throws PlantVersionException{
		PlantVersionDTO plantVersionDTO = null;
		double storageAmount;
		double unloadAmount;
		double grind;
		try{
			if(view != null){
				plantVersionDTO = new PlantVersionDTO();				
				storageAmount = NumberUtil.parseDouble(view.getStorageAmount());
				unloadAmount = NumberUtil.parseDouble(view.getUnloadAmount());
				grind = NumberUtil.parseDouble(view.getGrind());
				plantVersionDTO.setStorageAmount(storageAmount!=-1?storageAmount:0);
				plantVersionDTO.setUnloadAmount(unloadAmount!=-1?unloadAmount:0);
				plantVersionDTO.setGrind(grind!=-1?grind:0);
				plantVersionDTO.setGrindProtectionDays(view.getGrindProtectionDays());
				plantVersionDTO.setPlantVersionId(view.getPlantVersionId());				
			}
		}catch( Exception exception ){
			PlantVersionException plantVersionException=null;
			exception.printStackTrace(System.out);
			plantVersionException= new PlantVersionException(exception , CornPPException.LAYER_CONVERTER,CornPPException.ACTION_LISTS);
			throw plantVersionException;
		}
		return plantVersionDTO;
	}
}
